#ifndef __TASK_H_
#define __TASK_H_

#include <iostream>
#include "util.h"

class Task {
    public:
	TState_t doneState; /* state to depict end for deleting */      

 	pthread_mutex_t dmutex;
        pthread_cond_t tdone;
unsigned int id;  /*ID given for each task */
        unsigned int priority; /* a user assigned priority to the task in question */
       
        TState_t * myState; /* pointer to a state enum, can be used to manage children or to declare my state */
        
public:
	//bool done;
	

    

    Task() { /* default constructor */
	//	done = false;
            id = 0;
            doneState = Create;
            pthread_mutex_init(&dmutex, NULL);
            pthread_cond_init(&tdone, NULL);
        }

        Task(unsigned int i) {/* constructor */
	//	done = false;            
		id = i;
            doneState = Create;
            pthread_mutex_init(&dmutex, NULL);
            pthread_cond_init(&tdone, NULL);
        }

    void lock_d() {
        pthread_mutex_lock(&dmutex);
    } 
 
 void wait(){
	 pthread_cond_wait(&tdone, &dmutex);
	}

    void release_d() {
        pthread_mutex_unlock(&dmutex);
    }
void broadcast(){
 
pthread_cond_broadcast(&tdone);
       
}
	 virtual ~Task(){
        }

        virtual void run () = 0; // virtual function that the user must override //

        virtual void doneWithTask() {
            delete this;
        }

     
	TState_t GetState (){
	this->lock_d();
	TState_t x=doneState;
	this->release_d();
	return x;
		}


	void SetState (TState_t state){
	//std::cout<<" in function11 \n";	
	this->lock_d();		
	doneState=state;
	//std::cout<<" in function \n";
	this->release_d();
	//std::cout<<" in function2 \n";
} 

        virtual void DDone() {
	this->lock_d();
            doneState = Done;
	this->release_d();
	//std::cout<<" delete my self\n";
            delete this;
        }

        virtual void print() {// printing function for testing purposes //
        }

};

#endif 
